package com.kaigejava.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

/**
 * Created by kaigejava on 2019/7/21.
 * 消费者限流的消费端
 */
public class Cosumer4Limit {
    public static void main(String[] args) throws  Exception {



        //1 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.31.130");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2 获取C	onnection
        Connection connection = connectionFactory.newConnection();

        //3 通过Connection创建一个新的Channel
        Channel channel = connection.createChannel();

        String exchangeName = "test_limit_exchange";
        String routingKey = "limit.#";
        String queueName = "test_test_limit_queue";

        //4 声明交换机和队列 然后进行绑定设置, 最后制定路由Key
        channel.exchangeDeclare(exchangeName, "topic", true);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        //5 创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);

        //设置限流信息
        channel.basicQos(0,1,false);
       //设置限流的话必须设置为手动签收，将autoAck设置为false
        channel.basicConsume(queueName, false, new MyConsumer(channel));

    }
}

